package com.sms.controller;

import com.sms.pojo.ClassInfo;
import com.sms.pojo.ClassStudy;
import com.sms.pojo.CourseResult;
import com.sms.pojo.StudentInfo;
import com.sms.service.ClassInfoService;
import com.sms.service.ClassStudyService;
import com.sms.service.CourseResultService;
import com.sms.service.StudentInfoService;
import com.sms.utils.CheckDuplicate;
import com.sms.utils.RunEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
public class AdminController {
    @Autowired
    ClassInfoService classInfoService;
    @Autowired
    CourseResultService courseResultService;
    @Autowired
    StudentInfoService studentInfoService;
    @Autowired
    ClassStudyService classStudyService;
    //进入班级信息页面
    @RequestMapping("/Cinformation")
    public String Cinformation(Model model) {
        List<ClassStudy> classStudies = classStudyService.selectClassStudyAll();
        model.addAttribute("classStudies",classStudies);
        return "class_information";
    }
    //按班级名称查找班级
    @RequestMapping("/findclass")
    public String findclass(Integer classId,Model model) {
        ClassStudy classStudies;
        if(classId!=null){
            classStudies = classStudyService.selectClassStudyOne(classId);
        }
        else return "redirect:/Cinformation";
        model.addAttribute("classStudies",classStudies);
        return "class_information";
    }
    //删除班级
    @RequestMapping("/deleteclass")
    public String deleteclass(Integer classId,Model model) {
        classInfoService.deleteClass(classId);
        return "redirect:/Cinformation";
    }

    //进入班级
    @RequestMapping("/enteringclass")
    public String enteringclass(Integer classId,Model model) {
        List<StudentInfo> studentInfos = classStudyService.selectStudentClass(classId);
        model.addAttribute("studentInfos",  studentInfos);
        model.addAttribute("classId", classId);
        return "class_entering_class";
    }
    //按学号查找学生
    @RequestMapping("/findsid")
    public String findsid(Integer classId, Integer sid, Model model) {
        StudentInfo studentInfos;
        model.addAttribute("classId", classId);
        System.out.println(classId);
        if(sid!=null){
            studentInfos = studentInfoService.selectStudentById(sid);
        }else{
            return "redirect:/enteringclass?classId=" + classId;
        }
        model.addAttribute("studentInfos",  studentInfos);
        return "class_entering_class";
    }
    //在课程页面点击进入学生页面
    @RequestMapping("/cseestudent")
    public String cseestudent(Integer sid,Model model) {
        List<CourseResult> courseResults = courseResultService.selectCourseResultBySid(sid);
        model.addAttribute("sid",sid);
        model.addAttribute("courseResults",courseResults);
        return "class_see_student";
    }
    //删除学生
    @RequestMapping("/deletestudent")
    public String deletestudent(Integer sid,Integer classId) {
        System.out.println(classId);
        studentInfoService.deleteStudent(sid);
        return "redirect:/enteringclass?classId="+classId;
    }
    //在修改成绩
    @RequestMapping("/updategrade")
    public String updategrade(Integer sid,CourseResult courseResult,Model model) {
        try{
            courseResultService.updateCourseResult(courseResult);
        }catch (Exception e){
            return "redirect:/cseestudent?sid="+sid;
        }
        return "redirect:/cseestudent?sid="+sid;
    }

    //进入学生信息页面
    @RequestMapping("/CSinformation")
    public String CSinformation(Model model) {
        StudentInfo studentInfo = new StudentInfo();
        model.addAttribute("studentInfo", studentInfo);
        return "class_student_information";
    }
    //按学号查找学生
    @RequestMapping("/AdminfindSid")
    public String AdminfindSid(Integer sid,Model model) {
        StudentInfo studentInfo;
        if(sid!=null){
            studentInfo = studentInfoService.selectStudentById(sid);
        }else return "redirect:/CSinformation";
        if(studentInfo==null) return "redirect:/CSinformation";
        List<ClassStudy> classs = classStudyService.selectClassStudyAll();
        model.addAttribute("classs",classs);
        model.addAttribute("studentInfo",  studentInfo);
        return "class_student_information";
    }
    //提交学生信息页面
    @RequestMapping("/AdminupSid")
    public String AdminupSid(StudentInfo studentInfo,Model model) {
        try{
            studentInfoService.updateStudent(studentInfo);
            List<ClassStudy> classs = classStudyService.selectClassStudyAll();
            model.addAttribute("classs",classs);
        }catch (Exception e){
            return "redirect:/CSinformation";
        }
        model.addAttribute("studentInfo",studentInfo);
        return "class_student_information";
    }

    //进入添加学生页面
    @RequestMapping("/cainformation")
    public String cainformation(StudentInfo studentInfo,Model model) {
        List<ClassStudy> classs = classStudyService.selectClassStudyAll();
        model.addAttribute("classs",classs);
        return "class_add_student";
    }
    //管理员添加学生信息
    @RequestMapping("/adminaddstu")
    public String adminaddstu(StudentInfo studentInfo,Model model) {
        try{
            studentInfoService.insertStudent(studentInfo);
        }catch (Exception e){
            return "redirect:/cainformation";
        }
        return "redirect:/cainformation";
    }
    //进入添加班级页面
    @RequestMapping("/classaddclass")
    public String classaddclass() {
        return "class_add_class";
    }
    //进入添加班级页面
    @RequestMapping("/admininsertclass")
    public String admininsertclass(ClassInfo classInfo) {
        try {
            classInfoService.insertClass(classInfo);
        }catch (Exception e){
            return "class_add_class";
        }
        return "class_add_class";
    }
    //进入作业查重页面
    @RequestMapping("/classHomework")
    public String classHomework() {
        return "class_homework";
    }
    //处理查重
    @RequestMapping("/Duplicatecheck")
    public String Duplicatecheck(MultipartFile word, MultipartFile doc, Model model) {
        String name = word.getOriginalFilename();
        String name1 = doc.getOriginalFilename();
        model.addAttribute("name",name);
        model.addAttribute("name1",name1);
        Double dou;
        try{
            dou = CheckDuplicate.selectWordFile(word,doc);
        }catch (Exception e){
            return "redirect:/classHomework";
        }
        model.addAttribute("dou",String.format("%.2f%%", dou * 100));
        return "class_chachong";
    }
}
